package com.exec;

import com.alibaba.fastjson.JSONObject;
import com.tycmc.util.RedisUtil;
import com.util.DateUtil;
import com.util.DruidUtil;
import com.util.PropertiesUtil;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import ty.pub.TransPacket;

import java.sql.Connection;
import java.util.*;

/*
*缓存数据处理
 */
public class CacheMsgController {
    /**
     * 缓存数据
     * @param inputObject
     * @return
     */
    public  static String execCacheData(com.tycmc.bean.InputObject inputObject)  {
        //判断inputObject中是否存在这个工况信息
        TransPacket transPacket= inputObject.getTransPacket();
        //获取基础数据
        Map<String,String> baseInfo=transPacket.getBaseInfoMap();
        //获取工况数据
        Map<String,Map<Long,String>>workStatusMap=transPacket.getWorkStatusMap();
        //信息生成时间工况编号
        String msgTimeNumber=PropertiesUtil.getStringPro("MsgTimeNumber");
        String CacheMsg=PropertiesUtil.getStringPro("CacheMsg");
        //获取信息生成时间
        long msgTime=MapUtils.getLong(baseInfo,msgTimeNumber);
        //获取设备ID
        String vclId = transPacket.getDeviceId();
        vclId = vclId.substring(4, vclId.length());
        long reciveTime=transPacket.getTimestamp();
        //获取信息生成时间数据集合
        Map<String,Object>mapStatic=inputObject.getCacheMap();
        String theVclmsgTime=MapUtils.getString(mapStatic,vclId+"_"+CacheMsg);
        List<String>sqlList=new ArrayList<String>();
        //如果为空，直接存入缓存map中
        if(StringUtils.isBlank(theVclmsgTime)){
            mapStatic.put(vclId+"_"+CacheMsg,DateUtil.getStrTime(msgTime,0));
            sqlList.add(" insert into  CacheKZExecTable(CKZET_VclId,CKZET_LastMsgTime,CKZET_InsertTime,CKZET_Type)values('"+vclId+"','"+DateUtil.getStrTime(msgTime,0)+"',now(),"+CacheMsg+")  " +
                    "ON DUPLICATE KEY UPDATE CKZET_LastMsgTime='"+DateUtil.getStrTime(msgTime,0)+"',CKZET_InsertTime=now()");
        }else{//如果不为空
            //上下时间比较
            long lastVclmsgTime=Long.valueOf(DateUtil.getLongTime(theVclmsgTime));
            if(msgTime<lastVclmsgTime){
                //判断是否为当天日期，当天日期不存储
                if(DateUtil.getStrTime(msgTime).equals(DateUtil.getStrTime(System.currentTimeMillis()))){
                    return null;
                }
                long deff1=lastVclmsgTime-msgTime;
                //上下时间比较
                long deff2=reciveTime-msgTime;
                deff2=deff2/1000/86400;
                //前后超过1分钟，且信息生成时间在1年内的算属于缓存
                if(deff1>60000&&deff2<=365){
                    Map<String,Object>mapCache=(Map<String,Object>)mapStatic.get("cache");
                    if(mapCache==null){
                        mapCache=new HashMap<String,Object>();
                        mapStatic.put("cache",mapCache);
                    }
                    mapCache.put(vclId+"_"+DateUtil.getStrTime(msgTime),1 );
                   /* sqlList.add(" insert into  CacheLSExecTable(CLSET_VclId,CLSET_LastMsgTime,CLSET_InsertTime,CLSET_InsertDate,CLSET_Flag)values('"+vclId+"','"+DateUtil.getStrTime(msgTime)+"',now(),now(),0)" +
                            " ON DUPLICATE KEY UPDATE CLSET_InsertTime=now()");*/
                    sqlList.add(" insert into  CacheLSExecTable(CLSET_VclId,CLSET_LastMsgTime,CLSET_InsertTime,CLSET_InsertDate,CLSET_Flag)values('"+vclId+"','"+DateUtil.getStrTime(msgTime)+"',now(),now(),0)");
                }
            }else{
                mapStatic.put(vclId+"_"+CacheMsg,DateUtil.getStrTime(msgTime,0));
                sqlList.add(" insert into  CacheKZExecTable(CKZET_VclId,CKZET_LastMsgTime,CKZET_InsertTime,CKZET_Type)values('"+vclId+"','"+DateUtil.getStrTime(msgTime,0)+"',now(),"+CacheMsg+")  " +
                        "ON DUPLICATE KEY UPDATE CKZET_LastMsgTime='"+DateUtil.getStrTime(msgTime,0)+"',CKZET_InsertTime=now()");
            }
        }
        if(sqlList.size()>0){
            DruidUtil.batch(sqlList);
        }
        return null;
    }
}
